package com.me.jingdongshop.ui.holder;

import android.content.Context;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.me.common.base.OttoBus;
import com.me.common.ui.BaseAdapterRV;
import com.me.common.ui.BaseHolderRV;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.LocationList;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.ui.adapter.BottomAddressAdapter;

/**
 * Created by YL on 2017/5/9.
 */
public class BottomAddressHolder extends BaseHolderRV<LocationList.AddressListBean> {
    private TextView goodsAddressName;
    private ImageView goodsAddressYes;

    public BottomAddressHolder(Context context, ViewGroup parent, BaseAdapterRV<LocationList.AddressListBean> adapter, int itemType) {
        super(context, parent, adapter, itemType, R.layout.item_goods_address);
    }

    @Override
    public void onFindViews(View itemView) {
        goodsAddressName = (TextView) itemView.findViewById(R.id.goods_address_name);
        goodsAddressYes = (ImageView) itemView.findViewById(R.id.goods_address_yes);
    }

    @Override
    protected void onRefreshView(final LocationList.AddressListBean bean, int position) {
        final String text =  bean.getProvince() + bean.getCity() + bean.getAddressArea() + bean.getAddressDetail();
        goodsAddressName.setText(text);
        if(((BottomAddressAdapter)adapter).mPo == bean.getId()){
            goodsAddressYes.setVisibility(View.VISIBLE);
            goodsAddressName.setSelected(true);
        }else {
            goodsAddressYes.setVisibility(View.INVISIBLE);
            goodsAddressName.setSelected(false);
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BottomAddressAdapter)adapter).mPo = bean.getId();
                Message message = new Message();
                message.what = IHttpService.GOODS_ADDRESS_CODE;
                message.obj = text;
                OttoBus.getDefault().post(message);
            }
        });
    }

}
